//
//  UIColor+extend.m
//  T-BoxManager
//
//  Created by 车音 on 2019/6/24.
//  Copyright © 2019 CheYin. All rights reserved.
//

#import "UIColor+extend.h"

@implementation UIColor(extend)
- (NSString *)hexValue {
    UIColor *color = self;
    
    if (CGColorGetNumberOfComponents(color.CGColor) < 4) {
        const CGFloat *components = CGColorGetComponents(color.CGColor);
        color = [UIColor colorWithRed:components[0]
                                green:components[0]
                                 blue:components[0]
                                alpha:components[1]];
    }
    
    if (CGColorSpaceGetModel(CGColorGetColorSpace(color.CGColor)) != kCGColorSpaceModelRGB) {
        return [NSString stringWithFormat:@"#FFFFFF"];
    }
    CGFloat r = (CGColorGetComponents(color.CGColor))[0];
    CGFloat g = (CGColorGetComponents(color.CGColor))[1];
    CGFloat b = (CGColorGetComponents(color.CGColor))[2];
    return [[NSString stringWithFormat:@"#%2x%2x%2x", (int)(r*255.0), (int)(g*255.0), (int)(b*255.0)] stringByReplacingOccurrencesOfString:@" " withString:@"0"];
}

+ (UIColor *)hexFromHexString:(NSString *)hexString {
    NSAssert(hexString.length == 7 || hexString.length == 9, @"hexColor is invalid");
    NSAssert([hexString hasPrefix:@"#"], @"hexColor is invalid");
    //    hexColor = [hexColor stringByReplacingOccurrencesOfString:@"#" withString:@""];
    //    if ([hexColor length] != 6) {
    //        return [UIColor whiteColor];
    //    }
    unsigned int red, green, blue;
    unsigned int alpha = 255.0f;
    NSRange range;
    range.location = 1;
    range.length = 2;
    if (hexString.length == 9) { //有alpha 取alpha
        [[NSScanner scannerWithString:[hexString substringWithRange:range]] scanHexInt:&alpha];
        range.location += 2;
    }
    
    [[NSScanner scannerWithString:[hexString substringWithRange:range]] scanHexInt:&red];
    range.location += 2;
    
    [[NSScanner scannerWithString:[hexString substringWithRange:range]] scanHexInt:&green];
    range.location += 2;
    
    [[NSScanner scannerWithString:[hexString substringWithRange:range]] scanHexInt:&blue];
    
    return [UIColor colorWithRed:(float)(red/255.0f)
                           green:(float)(green/255.0f)
                            blue:(float)(blue/255.0f)
                           alpha:(float)(alpha/255.0f)];
}


+ (UIColor *)hexChangeFloat:(NSString *) hexColor {
    if ([CYUtils isEmptyString:hexColor]) {
        return [UIColor whiteColor];
    }
    
    NSString *colorString = [NSString stringWithString:hexColor];
    hexColor = [colorString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    
    if ([hexColor length] == 8) {
//        hexColor = [hexColor substringFromIndex:2];
    } else if ([hexColor length] != 6) {
        return [UIColor whiteColor];
    }
    
	unsigned int redInt_, greenInt_, blueInt_, alphaInt_;
	NSRange rangeNSRange_;
	rangeNSRange_.length = 2;  // 范围长度为2
	
    NSInteger location = 0;
    
    if (hexColor.length == 8) {
        rangeNSRange_.location = location;
        [[NSScanner scannerWithString:[hexColor substringWithRange:rangeNSRange_]]
         scanHexInt:&alphaInt_];
        location += 2;
    } else {
        alphaInt_ = 255.0f;
    }
    
	// 取红色的值
	rangeNSRange_.location = location;
	[[NSScanner scannerWithString:[hexColor substringWithRange:rangeNSRange_]] 
	 scanHexInt:&redInt_];
    location += 2;
	
	// 取绿色的值
	rangeNSRange_.location = location;
	[[NSScanner scannerWithString:[hexColor substringWithRange:rangeNSRange_]] 
	 scanHexInt:&greenInt_];
    location += 2;
	
	// 取蓝色的值
	rangeNSRange_.location = location;
	[[NSScanner scannerWithString:[hexColor substringWithRange:rangeNSRange_]] 
	 scanHexInt:&blueInt_];
    
	return [UIColor colorWithRed:(float)(redInt_/255.0f) 
						   green:(float)(greenInt_/255.0f) 
							blue:(float)(blueInt_/255.0f) 
						   alpha:(float)(alphaInt_/255.0f)];
}

+ (UIColor *)hexChangeFloat:(NSString *)hexColor alpha:(CGFloat)alpha {
    NSAssert(hexColor!= nil, @"hexColor is nil");
    if (!hexColor) {
        return [UIColor whiteColor];
    }
    NSString *colorString = [NSString stringWithString:hexColor];
    colorString = [colorString stringByReplacingOccurrencesOfString:@"#" withString:@""];
    if ([colorString length] != 6) {
        return [UIColor whiteColor];
    }
    
    unsigned int redInt_, greenInt_, blueInt_;
    NSRange rangeNSRange_;
    rangeNSRange_.length = 2;  // 范围长度为2
    
    // 取红色的值
    rangeNSRange_.location = 0;
    [[NSScanner scannerWithString:[colorString substringWithRange:rangeNSRange_]]
     scanHexInt:&redInt_];
    
    // 取绿色的值
    rangeNSRange_.location = 2;
    [[NSScanner scannerWithString:[colorString substringWithRange:rangeNSRange_]]
     scanHexInt:&greenInt_];
    
    // 取蓝色的值
    rangeNSRange_.location = 4;
    [[NSScanner scannerWithString:[colorString substringWithRange:rangeNSRange_]]
     scanHexInt:&blueInt_];
    
    if (alpha > 1.0f) {
        alpha = 1.0f;
    }
    if (alpha < 0.0f) {
        alpha = 0.0f;
    }
    
    return [UIColor colorWithRed:(float)(redInt_/255.0f)
                           green:(float)(greenInt_/255.0f)
                            blue:(float)(blueInt_/255.0f) 
                           alpha:alpha];
}

+ (UIColor *)colorWithHex:(int)hexColor
{
    float  a = 1.0;
    if(hexColor > 0xffffff || (hexColor >= 0x80ffffff && hexColor < 0))
    {
        a = 1.0 - ((hexColor & 0xff000000) >> 16 >> 8) * 1.f / 255;
    }
    float r = ((hexColor & 0x00ff0000) >> 16) * 1.f / 255;
    float g = ((hexColor & 0x0000ff00) >> 8) * 1.f / 255;
    float b = (hexColor & 0x000000ff) * 1.f / 255;
    return [UIColor colorWithRed:r green:g blue:b alpha:a];
}

+ (UIColor *)randomColor
{
    CGFloat hue = ( arc4random() % 256 / 256.0 ); //0.0 to 1.0
    CGFloat saturation = ( arc4random() % 128 / 256.0 ) + 0.5; // 0.5 to 1.0,away from white
    CGFloat brightness = ( arc4random() % 128 / 256.0 ) + 0.5; //0.5 to 1.0,away from black
    return [UIColor colorWithHue:hue saturation:saturation brightness:brightness alpha:1];
}

@end
